import {Component, HostListener, ElementRef, Renderer, ViewContainerRef} from '@angular/core';
import {ActivatedRoute, Router, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from '@angular/router';
import {UserLoginService} from './user/user-login/user-login.service';
import {UserRegisterService} from './user/user-register/user-register.service';
import {User} from './user/model/user-model';
import 'rxjs/add/operator/merge';
import {Http, Response} from '@angular/http';
import {HttpClient} from '@angular/common/http';
import {ElNotificationService} from 'element-angular/release/notification/notification.service';
import {Jsonp} from "@angular/http";
import 'rxjs/add/operator/map';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  public currentUser: User;
  private globalClickCallbackFn: Function;
  private loginSuccessCallbackFn: Function;


  constructor(
    public elementRef: ElementRef,
    private notify: ElNotificationService,
    public renderer: Renderer,
    public router: Router,
    public activatedRoute: ActivatedRoute,
    public userLoginService: UserLoginService,//登录服务
    public userRegisterService: UserRegisterService,//注册服务
    public http: Http,
    public httpClint:HttpClient,
    public jsonp:Jsonp
  ) {

  }

  ngOnInit() {
    console.log("app模块初始化")
    // this.globalClickCallbackFn = this.renderer.listen(this.elementRef.nativeElement, 'click', (event: any) => {
    //   console.log('全局监听点击事件>' + event.target);
    // });
    this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
    console.log('当前用户:', this.currentUser)
    this.userLoginService.currentUser                  //登录
      .merge(this.userRegisterService.currentUser)    //注册
      .subscribe(
        data => {
          console.log("从主组件改变")
          this.currentUser = data;
          let activatedRouteSnapshot: ActivatedRouteSnapshot = this.activatedRoute.snapshot;
          let routerState: RouterState = this.router.routerState;
          let routerStateSnapshot: RouterStateSnapshot = routerState.snapshot;

          console.log('----------------ActivatedRouteSnapshot:', activatedRouteSnapshot);
          console.log(routerState);
          console.log(routerStateSnapshot);

          //如果是从/login这个URL进行的登录，跳转到首页，否则什么都不做
          if (routerStateSnapshot.url.indexOf('/login') != -1) {
            this.router.navigateByUrl('/home');
          }
        },
        error => console.error(error)
      );

  }

  ngOnDestroy() {
    if (this.globalClickCallbackFn) {
      this.globalClickCallbackFn();
    }
  }

  toggle(button: any) {
    console.log(button);
  }


  doLogout(type: string): void {
    this.userLoginService.logout();
    this.notify[type]('成功退出: ' + type, type)
    this.router.navigateByUrl('');
  }

  handle(index: string): void {
    console.log(index)
  }


  testclick(){

    console.log("-------dasdasdas------>", this.http.get("/api/Blog/GetBlog").subscribe(data => {
      console.log(data);
    }))

    // this.jsonp.get("4200:mock-data/comment-mock.json").map(res => res.json()).subscribe(data => {
    //   // data为返回值
    //   console.log("Jsonp----------------->",data);
    // });
    // http://127.0.0.1:1991/api/SampleData/WeatherForecasts
    return "";
  }

}
